<?php
class Admin_Model_Route extends TBB_Model_Route
{
	/**
	 * Model name
	 * @var string
	 */
	protected $_name = 'route';
	
	/**
	 * Primary column
	 * @var string
	 */
	protected $_primary = 'route_id';
	
	/**
	 * Get route paginator adapter
	 * @param string $filtingField
	 * @param string $filtingCriteria
	 * @param string $sortingField
	 * @return Zend_Paginator_Adapter_DbTableSelect
	 */
	public function getRoutePaginatorAdapter($filtingField, $filtingCriteria, $sortingField)
	{
		$select = $this->select();
		if(!empty($filtingField)) {
			$select->where($filtingField . ' = ?', $filtingCriteria);
		}
		
		if(!empty($sortingField)) {
			$select->order($sortingField);
		} else {
			$select->order(array('from_city', 'to_city', 'boarding_point', 'dropping_point'));
		}
		
		$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
		return $adapter;
	}
	
	/**
	 * Check if the route existed
	 * @param string $from
	 * @param string $to
	 * @return string
	 */
	protected function _routeExists($from, $to)
	{
		$select = $this->select()->from('route')->where('from_city = ?', $from)->where('to_city = ?', $to);
		$r = $this->fetchRow($select);
		return empty($r) ? false : true;
	}

	/**
	 * Create route
	 * @param string $from
	 * @param string $to
	 * @param string $boardingPoint
	 * @param string $droppingPoint
	 * @return mixed
	 */
	public function createRoute($from, $to, $boardingPoint, $droppingPoint)
	{			
		$routeRow = $this->createRow();
		if($routeRow) {
			if($from == $to) throw new Exception('Could not create route. From and to must be different.');
			if($boardingPoint == $droppingPoint) throw new Exception('Could not create route. Boarding point and dropping point must be different.');
			if($this->_routeExists($from, $to)) throw new Exception('Could not create route. The Route existed.');			
			$routeRow->from_city = $from;
			$routeRow->to_city = $to;
			$routeRow->boarding_point = $boardingPoint;
			$routeRow->dropping_point = $droppingPoint;
			try {				
				return $routeRow->save();
			} catch (Exception $e){
				throw $e;
			}
		}
		throw new Exception('Could not create route. Route not found');
	}
	
	/**
	 * Update route
	 * @param int $id
	 * @param string $from
	 * @param string $to
	 * @param string $boardingPoint
	 * @param string $droppingPoint
	 * @return mixed
	 */
	public function updateRoute($id, $from, $to, $boardingPoint, $droppingPoint)
	{
		$routeRow = $this->find($id)->current();
		if($routeRow) {
			$routeRow->from_city = $from;
			$routeRow->to_city = $to;
			$routeRow->boarding_point = $boardingPoint;
			$routeRow->dropping_point = $droppingPoint;
			try {			
				return $routeRow->save();
			} catch (Exception $e) {
				throw $e;
			}			
		}
		throw new Exception('Could not update route. Route not found');		
	}
	
	/**
	 * Delete route
	 * @param $id
	 * @return int
	 */
	public function deleteRoute($id)
	{
		$routeRow = $this->find($id)->current();
		if($routeRow) {
			try {
				return $routeRow->delete();
			} catch(Exception $e) {
				throw $e;
			}
		}
		throw new Exception('Could not delete route');
	}
	
	/**
	 * Get route array with default order
	 * @return array
	 */
	private function _getRouteArray()
	{
		$select = $this->select()->order(array('from_city', 'to_city', 'boarding_point', 'dropping_point'));
		$r = $this->fetchAll($select);
		return $r ? $r->toArray() : array();
	}
	
	/**
	 * Get routeID-routeName map.
	 * @return array
	 */
	public function getRouteIDRouteNameMap() 
	{
		$routes = $this->_getRouteArray();
		$result = array();
		foreach($routes as $route) {
			$result[$route['route_id']] = $route['from_city'] . ' - ' . $route['to_city'];	
		}
		return $result;
	}
}